package com.example.liunanxin.lnx226ordering.model;


import com.example.liunanxin.lnx226ordering.beans.LoginBean;
import com.example.liunanxin.lnx226ordering.iface.LoginListener;
import com.example.liunanxin.lnx226ordering.iface.Loginiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.LoginService;
import com.example.liunanxin.lnx226ordering.url.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel implements Loginiface {
    private Retrofit retrofit;

    public LoginModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getLoginResult(String username, String userpass,final LoginListener loginListener) {
        LoginService loginService=retrofit.create(LoginService.class);
        Call<LoginBean> call=loginService.getUserid(username,userpass);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {

                if (response.isSuccessful()&response!=null){
                    loginListener.onResponse(response.body());
                }
                else loginListener.onFail("onrespons fail");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail(t.toString());

            }
        });

    }
}
